home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 483 / mkrscsrc / drag.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-26  |  9.0 KB  |  363 lines

  1. #include "stdio.h"
  2. #include "gemdefs.h"
  3. #include "obdefs.h"
  4. #include "osbind.h"
  5. #include "mkrsc.h"
  6. #include "globals.h"
  7.  
  8.  
  9. /*    dragging and resizing things in the window.  Index is the
  10.     object to be dragged/resized.
  11. */
  12.  
  13. drag_right(index,mx,my)
  14.     int        index,mx,my;
  15. {
  16.     int result, c, newstate, new_parent, parent;
  17.     int sw, sh, sx, sy, bw, bh, bx, by;
  18.     int xoff, yoff, an_mbox, next, xlim, ylim, nw, nh, nx, ny;
  19.     int finalx,finaly, finalw,finalh;
  20.     OBJECT *sobjptr;
  21.     OBJECT *subtrptr;
  22.     int    cx, cy, ms, kbd_state;
  23.  
  24.     xlim = ylim = 0;
  25.  
  26.     if(thefrontwin->inwindow->kind[0] == TMENU)
  27.         {    drag_menu(index,mx,my);
  28.             return;
  29.         }
  30.     subtrptr = thefrontwin->inwindow->objt;
  31.     sobjptr = &subtrptr[index];
  32.     an_mbox = thefrontwin->inwindow->mbox;
  33.  
  34.     /* need xoff yoff for dragbox */
  35.  
  36.     objc_offset(subtrptr, index, &xoff, &yoff);
  37.  
  38.     sx = xoff;    /* size of box to drag  */
  39.     sy = yoff;
  40.     sw = sobjptr->ob_width;
  41.     sh = sobjptr->ob_height;
  42.  
  43.     /* sets limits to where you can do things */
  44.  
  45.  
  46.             if(index == 0)
  47.             {    bx = thefrontwin->work.g_x;
  48.                 by = thefrontwin->work.g_y;
  49.                 bw = thefrontwin->work.g_w;
  50.                 bh = thefrontwin->work.g_h;
  51.             }
  52.             else
  53.             {    bx = subtrptr[0].ob_x;
  54.                 by = subtrptr[0].ob_y;
  55.                 bw = subtrptr[0].ob_width;
  56.                 bh = subtrptr[0].ob_height;
  57.             }
  58.         
  59.     newstate = subtrptr[index].ob_state | SELECTED;
  60.     objc_change(subtrptr,index,0,bx,by,bw,bh,newstate,1);
  61.  
  62. /*    check to see if mouse is near corner of object indicating that
  63.     you should do a graf_rubberbox in order to resize object.
  64. */
  65.  
  66.     if( (mx < (sx+sw)) && ((sx+sw-mx) < 7)
  67.             && (my < (sy+sh)) && ((sy+sh-my) < 7) )
  68.         
  69.         {    graf_rubberbox(sx,sy,gl_wchar,gl_hchar,&finalw,&finalh);
  70. /*    never make an object too small to encompass a child    */
  71.             if((next = subtrptr[index].ob_head) > 0)
  72.             {    while(next != index)
  73.                 {    xlim = max(xlim,subtrptr[next].ob_x +
  74.                                          subtrptr[next].ob_width);
  75.                     ylim = max(ylim,subtrptr[next].ob_y +
  76.                                          subtrptr[next].ob_height);
  77.                     next = subtrptr[next].ob_next;
  78.                 }
  79.             }
  80.             if(finalw < xlim)
  81.                 finalw = xlim;
  82.             if(finalh < ylim)
  83.                 finalh = ylim;
  84. /*  never allow an object outside its parent    */
  85.             if((parent = find_parent(subtrptr,index)) != -1)
  86.             { if(finalw > subtrptr[parent].ob_width - subtrptr[index].ob_x)
  87.                  finalw = subtrptr[parent].ob_width - subtrptr[index].ob_x; 
  88.               if(finalh > subtrptr[parent].ob_height - subtrptr[index].ob_y)
  89.                  finalh = subtrptr[parent].ob_height - subtrptr[index].ob_y; 
  90.             }
  91. /*    make sure we are inside the window or object 0    */
  92.             if(finalw > (bw - sx + bx))
  93.                 finalw = bw - sx + bx;
  94.             if(finalh > (bh - sy + by))
  95.                 finalh = bh - sy + by;
  96.             sobjptr->ob_height = finalh;
  97.             sobjptr->ob_width = finalw;
  98.             if(snap)
  99.             {    sobjptr->ob_width += gl_wchar/2;
  100.                 sobjptr->ob_width &= 0xFFF8;
  101.                 sobjptr->ob_height += gl_hchar/2;
  102.                 sobjptr->ob_height &=  (0xFFFF - gl_hchar + 1);
  103.             }
  104.             subtrptr[index].ob_state &= ~SELECTED;
  105.             return;
  106.         }
  107.  
  108. /*    Is the left-shift key down signifying a copy operation?    */
  109.  
  110.     graf_mkstate(&cx,&cy,&ms,&kbd_state); 
  111.     if( kbd_state == 0x2 )
  112.     {     if(copy_obj(1));
  113.             paste_obj();
  114.         return;
  115.     }
  116.  
  117. /*    can't move tree root   */
  118.  
  119.     if(index == 0) return;
  120.     
  121.     result = graf_dragbox(sw,sh,sx,sy,bx,by,bw,bh,&finalx,&finaly);
  122.     if(result == 0) return;
  123.  
  124. /*    now find parent over which the object has been dragged    */
  125.  
  126.     nw = sobjptr->ob_width;
  127.     nh = sobjptr->ob_height;
  128.  
  129.     new_parent = good_parent(subtrptr,finalx,finaly,nw,nh,index); 
  130.     if(new_parent == -1)
  131.         return;
  132.  
  133.     c = objc_offset(subtrptr, new_parent, &xoff, &yoff);
  134.     if(c == 0) return;
  135.  
  136.     sobjptr->ob_x = finalx - xoff;
  137.     sobjptr->ob_y = finaly - yoff;
  138.     if(snap)
  139.     {    sobjptr->ob_x += gl_wchar/2;
  140.         sobjptr->ob_x &= 0XFFF8;
  141.         sobjptr->ob_y += gl_hchar/2;
  142.         sobjptr->ob_y &= (0XFFFF - gl_hchar + 1);
  143.     }
  144.  
  145.     c = objc_delete(subtrptr,index);
  146.     c = objc_add(subtrptr,new_parent,index);
  147.     
  148.     /* deselect the object and redraw tree    */
  149.  
  150.     subtrptr[index].ob_state &= ~SELECTED;
  151.  
  152. }
  153.  
  154. /*    good_parent() finds the first parent for an object that completely
  155.     encompasses the object.  If it fails it returns -1.  x and y are the
  156.     upper left coordinates of the incomeing object, w and h its width
  157.     and height.  Index is its index number in the tree, if it is already
  158.     a member of the tree.  Make index = -2 if its a new object to be
  159.     added or pasted.
  160. */
  161.  
  162. int good_parent(objtr,x,y,w,h,index)
  163.     OBJECT    *objtr;
  164.     int    x,y,w,h,index;
  165. {    
  166.     int next, i, j, xoff, yoff, numobjs, nx, ny;
  167.     int order[MAXONUM];
  168.     int a, cx, cy, result, found;
  169.  
  170. /*    make a list of objects under the mouse in all_par[]    */
  171.  
  172.     numobjs = trav_tree(objtr,order);
  173.  
  174. /* find the topmost object that is under any corner of the object    */
  175.  
  176.     for(i= numobjs-1; i > -1; i--)
  177.     {    a = -1;
  178.         found = TRUE;
  179.         cx = x;
  180.         cy = y;
  181.         if((a = objc_find(objtr,order[i],0,cx,cy)) < 0)
  182.         {    cy = y + h;
  183.             if((a = objc_find(objtr,order[i],0,cx,cy)) < 0)
  184.             {    cx = x + w;
  185.                 if((a = objc_find(objtr,order[i],0,cx,cy)) < 0)
  186.                 {    cy = y;
  187.                     if((a = objc_find(objtr,order[i],0,cx,cy)) < 0)
  188.                     found = FALSE;
  189.                 }
  190.              }
  191.          }
  192.          
  193. /*    check to see if all four corners of snapped (or unsnapped) object
  194.     is over the potential parent a
  195. */
  196.         if(found)     
  197.         {    result = objc_offset(objtr, a, &xoff, &yoff);
  198.             if ( result == 0) return -1;
  199.             nx = x - xoff;
  200.             ny = y - yoff;
  201.             if(snap)
  202.             {    nx += gl_wchar/2;
  203.                 nx &= 0XFFF8;
  204.                 ny += gl_hchar/2;
  205.                 ny &= (0XFFFF - gl_hchar + 1);
  206.             }
  207.             if (    (nx >= 0)
  208.                 &&    (ny >= 0)
  209.                 &&    (objtr[a].ob_width >= (nx + w))    
  210.                 &&    (objtr[a].ob_height >= (ny + h))
  211.                 &&  (a != index)    )
  212.                 return(a);
  213.         }
  214.     }
  215.     return(-1);
  216. }
  217.  
  218. drag_menu(index,mx,my)
  219.     int        index,mx,my;
  220. {
  221.     int result, c, newstate;
  222.     int sw, sh, sx, sy, bw, bh, bx, by;
  223.     int xoff, yoff, an_mbox, next, xlim, ylim;
  224.     int finalx,finaly, finalw, finalh;
  225.     OBJECT *sobjptr;
  226.     OBJECT *subtrptr;
  227.     int titles[25], num_ti, i, doit, pos;
  228.     int    cx, cy, ms, kbd_state;
  229.     xlim = ylim = 0;
  230.  
  231.     subtrptr = thefrontwin->inwindow->objt;
  232.     sobjptr = &subtrptr[index];
  233.     an_mbox = thefrontwin->inwindow->mbox;
  234.  
  235.     /* need xoff yoff for dragbox */
  236.  
  237.     objc_offset(subtrptr, index, &xoff, &yoff);
  238.  
  239.     sx = xoff;    /* size of box to drag  */
  240.     sy = yoff;
  241.     sw = sobjptr->ob_width;
  242.     sh = sobjptr->ob_height;
  243.  
  244.     /* sets limits to where you can do things */
  245.  
  246.      if(sobjptr->ob_type == G_TITLE) 
  247.         {    objc_offset(subtrptr,1,&xoff,&yoff);
  248.             bx = xoff;
  249.             by = yoff;
  250.             bw = subtrptr[1].ob_width;
  251.             bh = subtrptr[1].ob_height;
  252.         }
  253.     else if (index == an_mbox)
  254.         {    bx = subtrptr[0].ob_x;
  255.             by = subtrptr[0].ob_y;
  256.             bw = subtrptr[0].ob_width;
  257.             bh = subtrptr[0].ob_height;
  258.         }
  259.     else    
  260.         {    objc_offset(subtrptr,an_mbox,&xoff,&yoff);
  261.             bx = xoff;
  262.             by = yoff;
  263.             bw = subtrptr[an_mbox].ob_width;
  264.             bh = subtrptr[an_mbox].ob_height;
  265.         }
  266.     
  267. /*    check to see if mouse is near corner of object indicating that
  268.     you should do a graf_rubberbox in order to resize object.
  269. */
  270.  
  271.     if( (mx < (sx+sw)) && ((sx+sw-mx) < 5)
  272.             && (my < (sy+sh)) && ((sy+sh-my) < 5) )
  273.         
  274.         {    newstate = subtrptr[index].ob_state & ~SELECTED;
  275.             objc_change(subtrptr,index,0,bx,by,bw,bh,newstate,1);
  276.             graf_rubberbox(sx,sy,gl_wchar,gl_hchar,&finalw,&finalh);
  277.  
  278. /*    never make an object too small to encompass a child    */
  279.  
  280.             if((next = subtrptr[index].ob_head) > 0)
  281.             {    while(next != index)
  282.                 {    xlim = max(xlim,subtrptr[next].ob_x +
  283.                                          subtrptr[next].ob_width);
  284.                     ylim = max(ylim,subtrptr[next].ob_y +
  285.                                          subtrptr[next].ob_height);
  286.                     next = subtrptr[next].ob_next;
  287.                 }
  288.             }
  289.             if(finalw < xlim)
  290.                 finalw = xlim;
  291.             if(finalh < ylim)
  292.                 finalh = ylim;
  293. /*    keep within the appropriate parent    */
  294.             if(finalw > (bw - sx + bx))
  295.                 finalw = bw - sx + bx;
  296.             if(finalh > (bh - sy + by))
  297.                 finalh = bh - sy + by;
  298.             sobjptr->ob_height = (finalh + gl_hchar/2);
  299.             sobjptr->ob_width = finalw + gl_wchar/2;
  300.             sobjptr->ob_width &= 0xFFF8;
  301.             sobjptr->ob_height &=  (0xFFFF - gl_hchar + 1);
  302.  
  303.             if(sobjptr->ob_type == G_TITLE)
  304.             {    sobjptr->ob_height = 0x13; 
  305.                 tidy_m();
  306.             }
  307.             return;
  308.         }
  309.  
  310. /*    Is the left-shift key down signifying a copy operation
  311.     of title or menu item?
  312. */
  313.  
  314.     graf_mkstate(&cx,&cy,&ms,&kbd_state); 
  315.     if( kbd_state == 0x2 )
  316.     {     if(copy_obj(1));
  317.             paste_obj();
  318.         return;
  319.     }
  320.  
  321. /* can't move current drop down box    */
  322.  
  323.     if(index == an_mbox) return;
  324.         
  325. /*    can't move menu objects 0, 1, and 2  */
  326.  
  327.     if(index < 3) return;
  328.     
  329.     result = graf_dragbox(sw,sh,sx,sy,bx,by,bw,bh,&finalx,&finaly);
  330.     if(result == 0) return;
  331.  
  332.     subtrptr[index].ob_state &= ~SELECTED;
  333.  
  334.      if( (sobjptr->ob_type == G_TITLE) && (index > 3 ) )
  335.         {
  336.             mv_ttl(index,finalx,finaly);   /*   in tree.c   */
  337.         }
  338.     else 
  339.         {
  340.  
  341. /* check to make sure that you are not overlaying items in mbox    */
  342.  
  343.     result = objc_find(subtrptr,0,10,finalx,finaly);
  344.     if(result != an_mbox)
  345.         return;
  346.     
  347.     c = objc_offset(subtrptr, an_mbox, &xoff, &yoff);
  348.     if(c == 0) return;
  349.  
  350.     /* place the object properly    */
  351.  
  352.     sobjptr->ob_x = finalx-xoff + gl_wchar/2;
  353.     sobjptr->ob_y = finaly-yoff+gl_hchar/2;
  354.     sobjptr->ob_x &= 0XFFF8;
  355.     sobjptr->ob_y &= (0XFFFF - gl_hchar + 1);
  356.  
  357.     /* reorder tree    */
  358.  
  359.     ord_tree();
  360.  
  361.         }        
  362. }
  363.